import CryptoJS from "crypto-js"; //引用AES源码js
const keyStr = `z0og7othowbpd6rx`;

function encrypt(word) {
    const key = CryptoJS.enc.Utf8.parse(keyStr)
    const srcs = CryptoJS.enc.Utf8.parse(word)
    const encrypted = CryptoJS.AES.encrypt(srcs, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    })
    return encrypted
}

export function decryptBase64(word) {
    var key = CryptoJS.enc.Utf8.parse(keyStr)
    var decrypt = CryptoJS.AES.decrypt(word, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    })
    return CryptoJS.enc.Utf8.stringify(decrypt).toString()
}

export function encryptHex(word) {
    const encrypted = encrypt(word, keyStr)
    return encrypted.ciphertext.toString().toUpperCase()
}

export function encryptBase64(word) {
    const encrypted = encrypt(word, keyStr)
    return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
}

export function decryptHex(hexWord) {
    const wordArray = CryptoJS.enc.Hex.parse(hexWord)
    const base64Word = CryptoJS.enc.Base64.stringify(wordArray)
    return decryptBase64(base64Word, keyStr)
}